package com.twobu.twobuinsight.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.twobu.twobuinsight.common.Result;
import com.twobu.twobuinsight.dto.IndustryDomainPageRequest;
import com.twobu.twobuinsight.entity.IndustryDomain;
import com.twobu.twobuinsight.service.IndustryDomainService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/industry-domain")
@Tag(name = "行业领域管理", description = "行业领域相关接口")
@Deprecated
public class IndustryDomainController {

    @Autowired
    private IndustryDomainService industryDomainService;

    @GetMapping("/list")
    @Operation(summary = "获取行业领域列表")
    public Result<Page<IndustryDomain>> list(@RequestParam(defaultValue = "1") Integer current,
                                            @RequestParam(defaultValue = "10") Integer size) {
        Page<IndustryDomain> page = new Page<>(current, size);
        return Result.success(industryDomainService.page(page));
    }

    @PostMapping("/page")
    @Operation(summary = "分页查询行业领域列表")
    public Result<Page<IndustryDomain>> page(@RequestBody IndustryDomainPageRequest request) {
        Page<IndustryDomain> page = new Page<>(request.getCurrent(), request.getSize());
        LambdaQueryWrapper<IndustryDomain> queryWrapper = new LambdaQueryWrapper<>();

        // 添加查询条件，如果参数不为空
        if (request.getIndustryDomainId() != null) {
            queryWrapper.eq(IndustryDomain::getIndustryDomainId, request.getIndustryDomainId());
        }
        if (StringUtils.hasText(request.getIndustryDomainName())) {
            queryWrapper.like(IndustryDomain::getIndustryDomainName, request.getIndustryDomainName());
        }

        // 按创建时间降序排序
        queryWrapper.orderByDesc(IndustryDomain::getCreateTime);

        return Result.success(industryDomainService.page(page, queryWrapper));
    }

    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取行业领域")
    public Result<IndustryDomain> getById(@PathVariable Long id) {
        return Result.success(industryDomainService.getById(id));
    }

    @PostMapping
    @Operation(summary = "添加行业领域")
    public Result<Boolean> add(@RequestBody IndustryDomain industryDomain) {
        return Result.success(industryDomainService.save(industryDomain));
    }

    @PutMapping
    @Operation(summary = "更新行业领域")
    public Result<Boolean> update(@RequestBody IndustryDomain industryDomain) {
        return Result.success(industryDomainService.updateById(industryDomain));
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除行业领域")
    public Result<Boolean> delete(@PathVariable Long id) {
        return Result.success(industryDomainService.removeById(id));
    }
}
